package com.smm.web.mtower.handler;


import com.smm.web.mtower.bo.EventBO;
import com.smm.web.mtower.domain.Event;
import com.smm.web.mtower.domain.TagAlamEvent;
import org.apache.log4j.Logger;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import javax.annotation.Resource;


/**
 * <p>
 * 	RFID基站信息监控类
 * </p>
 * @author 曾世华
 * @date 2016年1月21日
 * @version 1.0
 *
 */
public class MyHandler extends IoHandlerAdapter {
	
	private Logger logger= Logger.getLogger(this.getClass());

    @Resource
    private EventBO eventBO;


	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {

        String msg=(String) message;

        String rfidCode = msg.substring(6, 14);
        String shock = msg.substring(14, 16);
        logger.info("标签编号："+rfidCode+" 是否报警："+shock);

        try{

            //判断是是标签震动，如果不是震动事件，不做任何处理
            if(!shock.trim().equals(Event.Type_Code_Tag_Alam)) return;

            //处理报警事件
            eventBO.disposeAlamEvent(new TagAlamEvent(rfidCode));

        }catch (Exception e){

            e.printStackTrace();

            logger.error(e);
        }

	}


    @Override
	public void sessionClosed(IoSession session) throws Exception {
		super.sessionClosed(session);
        logger.info(session.getRemoteAddress().toString()+"断开连接.");
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status)
			throws Exception {
		super.sessionIdle(session, status);
	}
	
	/**
	 * 客户端建立连接
	 */
	@Override
	 public void sessionOpened(IoSession session) throws Exception {
		 logger.info(session.getRemoteAddress().toString()+"连接上来了.");
	 }

}
